import { useSub, useShare, useForm, useMenuTree } from '@/utils/comps'
import { useConfigStore } from '@/store/config'
import { useTtsaStore } from '@/store/ttsa'
import { useAppRouter } from '@/hooks'
import { useTrack } from '@/hooks'

export default function useNav() {
  const ttsaStore = useTtsaStore()
  const configStore = useConfigStore()
  const { setTrack } = useTrack()

  const navData = computed(() => configStore.navs)
  const funMap = {
    '0': (link) => {
      const [content_id, node_id] = link.split('_')
      ttsaStore.next({ content_id, node_id, jump_type: 'peek' })
    }, // 内容
    '1': useAppRouter, // 外链
    '2': useShare, // 分享
    '3': useSub, // 订阅
    '4': (id) => useForm(id, true), // 表单
    '5': useMenuTree, // 调取目录
  }

  const onClick = useDebounceFn((node) => {
    const { link_type, link, interrupt_digital } = node
    if (interrupt_digital && !ttsaStore.ttsa?.paused) ttsaStore.pause()
    if (Number(link_type) === 2) {
      // 统计分享点击
      setTrack({
        name: 'interactive',
        type: 'share',
      })
    }
    funMap[link_type](link)
  }, 200)

  onBeforeMount(() => {
    if (configStore.nav_is_show) configStore.getNavConfig()
  })

  return {
    navData,
    onClick
  }
}